package com.winter.service.impl;


import com.winter.mapper.OrderInfoMapper;
import com.winter.mapper.TopicMapper;
import com.winter.mapper.UserMapper;
import com.winter.model.OrderInfo;
import com.winter.model.Topic;
import com.winter.model.User;
import com.winter.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderInfoService {

    @Autowired
    private OrderInfoMapper mapper;
    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private UserMapper userMapper;

    //接单
    @Override
    @Transactional
    public void getOrder(Map<String, Object> paramMap) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String outTradeOn = "98745781448787";//暂时使用
        long tid = (long) paramMap.get("tid");//帖子id
        long userId = (long) paramMap.get("userId");//提问者id
        long gunManId = (long) paramMap.get("GunmanId");//答题的枪手id
        BigDecimal orderFee = topicMapper.selectByPrimaryKey(tid).getFee().multiply(new BigDecimal(0.2));//枪手的定金
        BigDecimal fee = topicMapper.selectByPrimaryKey(tid).getFee();
        OrderInfo order = new OrderInfo();
        order.setOrderNumber(userId + sdf.format(new Date()) +(int)Math.random() * 999);//随机生成订单号用户id+日期+三位随机数
        order.setGunmanId(gunManId);//枪手id
        order.setBuyerId(userId);//提问者id
        order.setOrderAmount(orderFee);//定金
        order.setDelState(1);
        order.setOrderStatus(Byte.parseByte("1"));//新建订单默认未付款
        order.setPayTime(new Date());
        order.setOuterTradeon(outTradeOn);//第三方付款订单号
        order.setOrderAmount(fee);//提问者给出的赏金
        order.setPayAmount(fee);
        order.setTotalAmount(fee.multiply(new BigDecimal(0.8)));//枪手只能获得赏金的80%
        order.setIsMiddle(paramMap.containsKey("isMiddle")?(int)paramMap.get("isMiddle"):null);//是否有中期
        order.setIsCancel(0);//新增的订单为为撤销,方便以后开发后台订单管理模块
        order.setCreateDate(new Date());
        order.setCreateUser("ball");
        order.setUpdateDate(new Date());
        order.setUpdateUser("ball");
        order.setTopicId(tid);//帖子id;
        orderInfoMapper.insertOrder(order);

        //此时这个问题状态变为2解决中
        Topic topic = topicMapper.selectByPrimaryKey(tid);
        topic.setFlag(2);
        topicMapper.updateByPrimaryKeySelective(topic);

        //枪手接单以后收到定金
        User gunMan = userMapper.selectByPrimaryKey(gunManId);
        gunMan.setMoney(gunMan.getMoney().add(orderFee));
        userMapper.updateByPrimaryKeySelective(gunMan);

        //提问者接单以后支付定金
        User user = userMapper.selectByPrimaryKey(userId);
        user.setMoney(user.getMoney().subtract(orderFee));
        userMapper.updateByPrimaryKeySelective(user);

    }


    public int deleteByPrimaryKey(Long orderId) {
        return mapper.deleteById(orderId);
    }

    public int insertOrder(OrderInfo orderInfo) {


        return mapper.insertOrder(orderInfo);

    }

    public OrderInfo selectByPrimaryKey(Long orderId) {


        return mapper.selectByPrimaryKey(orderId);

    }


}
